﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Xml.Linq;

namespace ReporterApplication
{
    public partial class FormReporter : Form
    {
        public FormReporter()
        {
            InitializeComponent();
        }

        private void BrowseCIFFilebutton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(BrowseCIFFiletextBox.Text))
                ResultfolderBrowserDialog.SelectedPath = BrowseCIFFiletextBox.Text;

            var dialogResult = ResultfolderBrowserDialog.ShowDialog();

            if (dialogResult == DialogResult.OK || dialogResult == DialogResult.Yes)
            {
                BrowseCIFFiletextBox.Text = ResultfolderBrowserDialog.SelectedPath;

                saveSettings();
            }

        }

        private void BrowseTemplateFilebutton_Click(object sender, EventArgs e)
        {
            var dialogResult = WordopenFileDialog.ShowDialog();

            if (dialogResult == DialogResult.OK || dialogResult == DialogResult.Yes)
            {
                BrowseTemplateFiletextBox.Text = WordopenFileDialog.FileName;

                saveSettings();

                //using (var fileStream = WordopenFileDialog.OpenFile())
                //{

                //}
            }
        }

        private void BrowseReportFolderbutton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(BrowseReportFoldertextBox.Text))
                TemplateopenFileDialog.FileName = BrowseReportFoldertextBox.Text;

            var dialogResult = TemplateopenFileDialog.ShowDialog();

            if (dialogResult == DialogResult.OK || dialogResult == DialogResult.Yes)
            {
                BrowseReportFoldertextBox.Text = TemplateopenFileDialog.FileName;

                saveSettings();

                //using (var fileStream = TemplateopenFileDialog.OpenFile())
                //{

                //}
            }
        }

        private void CreateReportbutton_Click(object sender, EventArgs e)
        {
            CreateReportbutton.Enabled = false;

            progresslabel.Text = "Started";

            progressBar1.Value = 5;


            backgroundWorker1.RunWorkerAsync();


            //progressReporttimer.Enabled = true;


            CreateReportbutton.Enabled = false;





            //progressReporttimer.Enabled = false;
        }

        private void CifExportbutton_Click(object sender, EventArgs e)
        {
            ImportProvision ip = new ImportProvision();

            ip.Settings = new ReportSettings
            {
                CifFolder = BrowseCIFFiletextBox.Text,
                TemplateFile = BrowseTemplateFiletextBox.Text,
                ResultFile = BrowseReportFoldertextBox.Text
            };

            ip.ShowDialog();
        }

        int counter = 0;

        private void progressReporttimer_Tick(object sender, EventArgs e)
        {
            progressBar1.Value = XmlCIF.Progress;
            progresslabel.Text = XmlCIF.Progress.ToString() + "%";
            progressStepslabel.Text = XmlCIF.ProgressStep;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var settings = new ReportSettings
            {
                CifFolder = BrowseCIFFiletextBox.Text,
                TemplateFile = BrowseTemplateFiletextBox.Text,
                ResultFile = BrowseReportFoldertextBox.Text
            };

            ReportEngine re = new ReportEngine();

            re.Report(settings);

            
        }

      

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var settings = new ReportSettings
            {
                CifFolder = BrowseCIFFiletextBox.Text,
                TemplateFile = BrowseTemplateFiletextBox.Text,
                ResultFile = BrowseReportFoldertextBox.Text
            };

            if (openOnDonecheckBox.Checked)
            {
                try
                {
                    Process.Start(settings.ResultFile);
                }
                catch (Exception)
                {
                    MessageBox.Show("Result could not be open.", "ERROR",
         MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            //show report
            ReportstextBox.Text = ReportEngine.LogBuilder.ToString();

            CreateReportbutton.Enabled = true;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void showModelbutton_Click(object sender, EventArgs e)
        {
            ModelForm mf = new ModelForm();
            mf.CifFolder = BrowseCIFFiletextBox.Text;

            

            mf.Show();
        }

        private void BrowseCIFFiletextBox_TextChanged(object sender, EventArgs e)
        {
            saveSettings();
        }

        private void BrowseTemplateFiletextBox_TextChanged(object sender, EventArgs e)
        {
            saveSettings();
        }

        private void BrowseReportFoldertextBox_TextChanged(object sender, EventArgs e)
        {
            saveSettings();
        }

        private void openOnDonecheckBox_CheckedChanged(object sender, EventArgs e)
        {
            saveSettings();
        }

        void saveSettings()
        {
            UserSettings us = new UserSettings();

            us.OpenResultInWord = openOnDonecheckBox.Checked;
            us.ReportFolder = BrowseCIFFiletextBox.Text;
            us.ResultFile = BrowseReportFoldertextBox.Text;
            us.TemplateFile = BrowseTemplateFiletextBox.Text;

            UserSettings.Save(us);
        }

        void loadSettings()
        {

            UserSettings us = UserSettings.Load();

            openOnDonecheckBox.Checked = us.OpenResultInWord;

            if (!string.IsNullOrEmpty(us.ReportFolder))
                BrowseCIFFiletextBox.Text = us.ReportFolder;

            if (!string.IsNullOrEmpty(us.ResultFile))
                BrowseReportFoldertextBox.Text = us.ResultFile;

            if (!string.IsNullOrEmpty(us.TemplateFile))
                BrowseTemplateFiletextBox.Text = us.TemplateFile;
        }

        private void FormReporter_Load(object sender, EventArgs e)
        {
            Version vs = new Version(Application.ProductVersion);
            Version vs2 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

            //this.Text += "  " + vs.Build + "." + vs.Major + "." + vs.Revision;
            this.Text += "  "+vs2.Major + "." + vs2.Minor + "." + vs2.Build + "." + vs2.Revision;
            
            loadSettings();
        }

        private void DateFormattextBox_TextChanged(object sender, EventArgs e)
        {
            XmlCIF.DateFormat = DateFormattextBox.Text;
        }

        private void eval_button_Click(object sender, EventArgs e)
        {
            SelectorTestForm tf = new SelectorTestForm();

            tf.CifFolder = BrowseCIFFiletextBox.Text;

            tf.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ReporterEngine.test.tt2();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var form = new EvalForm();
            form.ShowDialog();
        }
    }
}
